package com.teoresi.sb.db.dao.impl;

import java.util.List;

import com.teoresi.sb.bean.GenericBean;
import com.teoresi.sb.db.dao.BasicDAO;
import com.teoresi.sb.db.dao.DAOSupport;

public class BasicDAOImpl<T extends GenericBean> extends DAOSupport implements BasicDAO<T> {

	private String prefix;
	
	@Override
	public List<GenericBean> selectAll() {
		List<GenericBean> list = null;
		openSession();
		try {
			list = session.selectList(getPrefix() + ".selectAll");
		} finally {
			closeSession();
		}
		return list;
	}

	@Override
	public GenericBean selectById(long id) {
		GenericBean cliente = null;
		openSession();
		try {
			cliente = session.selectOne(getPrefix() + ".selectById", id);

		} finally {
			closeSession();
		}
		return cliente;
	}

	@Override
	public long insert(GenericBean entita) {
		openSession();
		long id = -1;
		try {
			id = session.insert(getPrefix() + ".insert", entita);
		} finally {
			commitSession();
			closeSession();
		}
		return id;
	}

	@Override
	public void update(GenericBean entita) {
		openSession();
		try {
			session.update(getPrefix() + ".update", entita);

		} finally {
			commitSession();
			closeSession();
		}		
	}

	@Override
	public void delete(long id) {
		openSession();
		try {
			session.delete(getPrefix() + ".delete", id);
		} finally {
			commitSession();
			closeSession();
		}
	}

	public void setPrefix(String prefix) {
		this.prefix = prefix;
	}

	public String getPrefix() {
		return prefix;
	}

}
